package com.lanou.web.controller;

import com.github.pagehelper.PageHelper;
import com.lanou.entity.TbContent;
import com.lanou.service.ContentService;
import com.lanou.utils.ShiHuiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Created by QuChao on 2018/11/23
 */
@RestController
public class ContentController {
    @Autowired
    private ContentService contentService;

    @RequestMapping("/content/query/list")
    public List<TbContent> list(Long categoryId,
                                @RequestParam(value = "page", defaultValue = "1") Integer page,
                                @RequestParam(value = "rows", defaultValue = "20") Integer rows) {
        PageHelper.startPage(page, rows);
        List<TbContent> contentList = contentService.list(categoryId);

        return contentList;
    }

    @PostMapping("/content/save")//保存内容
    public Map<String, Object> saveContent(TbContent tbContent) {
        boolean result = contentService.save(tbContent);
        return result ? ShiHuiResult.ok() : ShiHuiResult.error(400);
    }

    @PostMapping("/rest/content/edit")//编辑内容
    public Map<String, Object> edit(TbContent tbContent) {
        boolean result = contentService.edit(tbContent);
        return result ? ShiHuiResult.ok() : ShiHuiResult.error(400);
    }

    @PostMapping("/content/delete")//删除内容
    public Map<String, Object> delete(Long[] ids) {
        boolean result = contentService.deleteByIds(ids);
        return result ? ShiHuiResult.ok() : ShiHuiResult.error(400);
    }

}
